package HouseRent.view;

import HouseRent.domain.House;
import HouseRent.service.HouseService;
import HouseRent.utils.Utility;

/**
 * Created with IntelliJ IDEA.
 * Description:HouseView表示一个视图界面，用来读取用户输入并将信息传给HouseService方法进行数据处理最终返回到屏幕上
 * User:lenovo
 * Date:2024-06-09
 * Time:17:42
 */
public class HouseView {
    private boolean loop=true;
    private char key=' ';
    //有空字符串，没有空字符
    private HouseService houseService=new HouseService(2);
    //初始化10个房屋

    public void exit(){
        char c = Utility.readConfirmSelection();
        if(c=='Y') {
            loop = false;
        }
    }
    public void listHouses(){
        System.out.println("===============房 屋 列 表===============");
        System.out.println("编号\t房主\t电话\t地址\t月租\t状态");
        House[] houses=houseService.list();
        for(int i=0;i< houses.length;i++){
            if(houses[i]==null){
                //如果为空，直接结束
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println("===============房屋列表显示完毕===============");
    }

    public void addHouse(){
        System.out.println("===============添加房屋===============");
        //id(系统分配),姓名，电话，地址，月租,状态
        System.out.print("姓名：");
        String name=Utility.readString(5);
        System.out.print("电话：");
        String telephone=Utility.readString(16);
        System.out.print("地址：");
        String address=Utility.readString(6);
        System.out.print("月租：");
        int rent=Utility.readInt();
        System.out.print("状态：");
        String state=Utility.readString(3);

        House newHouse=new House(0,name,telephone,address,rent,state);
        if(houseService.add(newHouse)){
            System.out.println("===============添加房屋成功===============");
        }else{
            System.out.println("===============添加房屋失败===============");
        }
    }

    public void delHouse(){
        System.out.println("===============删除房屋信息===============");
        System.out.println("请选择待删除房屋信息编号(-1退出):");
        int delId=Utility.readInt();
        if(delId==-1){
            System.out.println("===============放弃删除房屋信息===============");
            return;
        }
        //确认环节
        char choice=Utility.readConfirmSelection();//这个方法本身就有判定是否确定的逻辑
        if(choice=='Y'){
            //执行删除逻辑
            if(houseService.del(delId)){
                System.out.println("===============删除房屋信息成功===============");
            }else{
                System.out.println("===============删除房屋信息失败===============");
            }
        }else{
            System.out.println("===============放弃删除房屋信息===============");
        }
    }

    public void findHouse(){
        System.out.println("===============房屋出租系统菜单===============");
        System.out.println("请输入你要查找的房屋编号(-1退出)：");
        int findId=Utility.readInt();
        if(findId==-1)
            return;
        House house = houseService.findById(findId);
        if(house!=null){
            System.out.println(house);
        }else{
            System.out.println("===============所要查找房屋不存在===============");
        }
    }
    public void updateHouse(){
        System.out.println("===============修改房屋信息===============");
        System.out.println("请输入你要删除的房屋编号(-1表示退出):");
        int updateId=Utility.readInt();
        if(updateId==-1){
            System.out.println("===============放弃修改房屋信息===============");
            return;
        }

        House house=houseService.findById(updateId);

        if(house==null){
            System.out.println("===============所修改的房屋不存在===============");
            return;
        }
        //姓名，电话，地址，租金，状态
        System.out.print("姓名：("+house.getName()+"):");
        String name=Utility.readString(10,"");
        if(!"".equals(name)){
            house.setName(name);
        }
        System.out.print("电话：("+house.getTelephone()+"):");
        String telephone=Utility.readString(10,"");
        if(!"".equals(telephone)){
            house.setTelephone(telephone);
        }
        System.out.print("地址：("+house.getAddress()+"):");
        String address=Utility.readString(10,"");
        if(!"".equals(address)){
            house.setAddress(address);
        }
        System.out.print("租金：("+house.getRent()+"):");
        int rent=Utility.readInt(-1);
        if(rent!=-1){
            house.setRent(rent);
        }
        System.out.print("状态：("+house.getState()+"):");
        String state=Utility.readString(10,"");
        if(!"".equals(state)){
            house.setState(state);
        }
        System.out.println("===============修改房屋信息成功===============");
    }

    public void mainMenu(){
        //显示主菜单
        do{
            System.out.println("===============房屋出租系统菜单===============");
            System.out.println("\t\t1 新 增 房 源");
            System.out.println("\t\t2 查 找 房 源");
            System.out.println("\t\t3 删 除 房 屋 信 息");
            System.out.println("\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t5 房 屋 列 表");
            System.out.println("\t\t6 退      出");
            System.out.print("请输入你的选择（1-6）：");
            key= Utility.readChar();
            switch(key){
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
                    exit();
                    //也封装为一个方法，不然显得怪异
                    break;
            }
        }while(loop);
    }
}
